package com.xbongbong.pro.message.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.wrap.Entity;
import com.xbongbong.pro.message.constant.MessageSetDetailTitleConstants;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushParentTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.MessageSetDetailPojo;
import com.xbongbong.pro.message.pojo.dto.MessageSetDetailDTO;
import com.xbongbong.pro.message.pojo.vo.MessageSetDetailVO;
import com.xbongbong.pro.message.pojo.vo.PushSetVO;
import com.xbongbong.pro.message.pushset.InvoicePushSet;
import com.xbongbong.pro.message.pushset.InvoiceRedPushSet;
import com.xbongbong.pro.message.pushset.PayPlanPushSet;
import com.xbongbong.pro.message.pushset.PayPlanRemindOverduePushSet;
import com.xbongbong.pro.message.pushset.PayPlanRemindPushSet;
import com.xbongbong.pro.message.pushset.PaySheetBadPushSet;
import com.xbongbong.pro.message.pushset.PaySheetPrepaySet;
import com.xbongbong.pro.message.pushset.PaySheetPushSet;
import com.xbongbong.pro.message.pushset.PaySheetRedPrepaySet;
import com.xbongbong.pro.message.pushset.PaySheetRedPushSet;
import com.xbongbong.pro.message.pushset.PaymentPlanPushSet;
import com.xbongbong.pro.message.pushset.PaymentRemindOverduePushSet;
import com.xbongbong.pro.message.pushset.PaymentRemindPushSet;
import com.xbongbong.pro.message.pushset.PaymentSheetBadPushSet;
import com.xbongbong.pro.message.pushset.PaymentSheetPrepaymentSet;
import com.xbongbong.pro.message.pushset.PaymentSheetPushSet;
import com.xbongbong.pro.message.pushset.PaymentSheetRedPrepaymentSet;
import com.xbongbong.pro.message.pushset.PaymentSheetRedPushSet;
import com.xbongbong.pro.message.pushset.PurchaseInvoicePushSet;
import com.xbongbong.pro.message.pushset.PurchaseInvoiceRedPushSet;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.CustomerStatementStatisticsEntity;
import com.xbongbong.saas.domain.entity.InvoiceRelationshipEntity;
import com.xbongbong.saas.domain.entity.PurchaseInvoiceRelationshipEntity;
import com.xbongbong.saas.domain.entity.ext.ContractEntityExt;
import com.xbongbong.saas.domain.entity.ext.InvoiceEntityExt;
import com.xbongbong.saas.domain.entity.ext.PayPlanEntityExt;
import com.xbongbong.saas.domain.entity.ext.PayPlanSheetEntityExt;
import com.xbongbong.saas.domain.entity.ext.PaymentEntityExt;
import com.xbongbong.saas.domain.entity.ext.PaymentSheetEntityExt;
import com.xbongbong.saas.domain.entity.ext.PurchaseEntityExt;
import com.xbongbong.saas.domain.entity.ext.PurchaseInvoiceEntityExt;
import com.xbongbong.saas.enums.CustomerStatementStatisticDistributorEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.PayPlanEnum;
import com.xbongbong.saas.enums.business.PaySheetEnum;
import com.xbongbong.saas.enums.business.PaymentEnum;
import com.xbongbong.saas.enums.business.PaymentSheetEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.model.ContractModel;
import com.xbongbong.saas.model.ContractUserModel;
import com.xbongbong.saas.model.CustomerUserModel;
import com.xbongbong.saas.model.InvoiceRelationshipModel;
import com.xbongbong.saas.model.PurchaseInvoiceRelationshipModel;
import com.xbongbong.saas.model.PurchaseModel;
import com.xbongbong.saas.model.PurchaseUserModel;
import com.xbongbong.saas.model.SupplierUserModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 资金管理消息设置实现类
 *
 * @author LiZhikang
 * @date 2022/11/5 16:34
 */
@Service
public class FundManagementMessageSetServiceImpl extends AbstractMessageSetService {

    @Resource
    private ContractUserModel contractUserModel;
    @Resource
    private ContractModel contractModel;
    @Resource
    private PurchaseUserModel purchaseUserModel;
    @Resource
    private PurchaseModel purchaseModel;
    @Resource
    private InvoiceRelationshipModel invoiceRelationshipModel;
    @Resource
    private CustomerUserModel customerUserModel;
    @Resource
    private SupplierUserModel supplierUserModel;
    @Resource
    private PurchaseInvoiceRelationshipModel purchaseInvoiceRelationshipModel;

    @Override
    public List<String> alias() {
        return Collections.singletonList(XbbRefTypeEnum.FUND_MANAGEMENT.getAlias());
    }

    @Override
    public String initSetDetailList() throws Exception {
        return null;
    }

    @Override
    public MessageSetDetailVO getSetDetailList(MessageSetDetailDTO messageSetDetailDTO) throws Exception {
        List<MessageSetDetailPojo> messageSetDetailPojos = new ArrayList<>();
        Integer typeInt = messageSetDetailDTO.getSaasIds().get(0).intValue();
        typeInt = XbbRefTypeEnum.transferBusinessType4Distributor(typeInt, messageSetDetailDTO.getDistributorMark());
        String alias = PushTypeEnum.getByCode(typeInt).getAlias();
        if (!Objects.equals(alias, PushTypeEnum.UNKNOWN.getAlias())) {
            String methodName = "getMessageSetSaas" + alias.substring(0, 1).toUpperCase() + alias.substring(1) + "DetailList";
            Method method = this.getClass().getDeclaredMethod(methodName, String.class, List.class, Integer.class);
            method.invoke(this, messageSetDetailDTO.getCorpid(), messageSetDetailPojos, typeInt);
        }
        return new MessageSetDetailVO(messageSetDetailPojos);
    }


    /**
     * 应收款 701
     *
     * @param corpid
     * @param messageSetDetailPojos
     * @param typeInt
     * @throws IllegalAccessException
     * @throws XbbException
     * @throws InstantiationException
     */
    private void getMessageSetSaasPaymentPlanAddDetailList(String corpid, List<MessageSetDetailPojo> messageSetDetailPojos, Integer typeInt) throws IllegalAccessException, XbbException, InstantiationException {
        MessageSetDetailPojo detailPojo = new MessageSetDetailPojo();
        detailPojo.setTitle(I18nMessageUtil.getMessage(MessageSetDetailTitleConstants.PAYMENT_REMIND));
        List<PushSetEntity> basePushSetList = new ArrayList<>();

        PaymentPlanPushSet paymentPlanPushSet = new PaymentPlanPushSet();
        PushSetEntity paymentPlanPushSetEntity = paymentPlanPushSet.getPushSet(corpid, PushTypeEnum.PAYMENT_PLAN_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(paymentPlanPushSetEntity);

        PaymentRemindPushSet paymentRemindPushSet = new PaymentRemindPushSet();
        PushSetEntity paymentRemindPushSetEntity = paymentRemindPushSet.getPushSet(corpid, PushTypeEnum.PAYMENT_REMIND.getSubCode(), pushSetModel);
        basePushSetList.add(paymentRemindPushSetEntity);

        PaymentRemindOverduePushSet paymentRemindOverduePushSet = new PaymentRemindOverduePushSet();
        PushSetEntity paymentRemindOverduePushSetEntity = paymentRemindOverduePushSet.getPushSet(corpid, PushTypeEnum.PAYMENT_OVERDUE.getSubCode(), pushSetModel);
        basePushSetList.add(paymentRemindOverduePushSetEntity);

        List<PushSetVO> pushSetVOList = new ArrayList<>();
        BeanUtil.copyPropertiesList(basePushSetList, pushSetVOList, PushSetVO.class);
        detailPojo.setValue(pushSetVOList);
        messageSetDetailPojos.add(detailPojo);
    }

    /**
     * 回款单 702
     *
     * @param corpid
     * @param messageSetDetailPojos
     * @param typeInt
     * @throws IllegalAccessException
     * @throws XbbException
     * @throws InstantiationException
     */
    private void getMessageSetSaasPaymentSheetAddDetailList(String corpid, List<MessageSetDetailPojo> messageSetDetailPojos, Integer typeInt) throws IllegalAccessException, XbbException, InstantiationException {
        MessageSetDetailPojo detailPojo = new MessageSetDetailPojo();
        detailPojo.setTitle(I18nMessageUtil.getMessage(MessageSetDetailTitleConstants.PAYMENT_SHEET_REMIND));
        List<PushSetEntity> basePushSetList = new ArrayList<>();

        PaymentSheetPushSet paymentSheetPushSet = new PaymentSheetPushSet();
        PushSetEntity paymentSheetPushSetEntity = paymentSheetPushSet.getPushSet(corpid, PushTypeEnum.PAYMENT_SHEET_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(paymentSheetPushSetEntity);

        PaymentSheetRedPushSet paymentSheetRedPushSet = new PaymentSheetRedPushSet();
        PushSetEntity paymentSheetRedPushSetEntity = paymentSheetRedPushSet.getPushSet(corpid, PushTypeEnum.PAYMENT_SHEET_RED_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(paymentSheetRedPushSetEntity);

        PaymentSheetBadPushSet paymentSheetBadPushSet = new PaymentSheetBadPushSet();
        PushSetEntity paymentSheetBadPushSetEntity = paymentSheetBadPushSet.getPushSet(corpid, PushTypeEnum.PAYMENT_SHEET_BAD_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(paymentSheetBadPushSetEntity);

        PaymentSheetPrepaymentSet prepaymentPushSet = new PaymentSheetPrepaymentSet();
        PushSetEntity prepaymentPushSetEntity = prepaymentPushSet.getPushSet(corpid, PushTypeEnum.PAYMENT_SHEET_PREPAYMENT_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(prepaymentPushSetEntity);

        PaymentSheetRedPrepaymentSet redPrepaymentPushSet = new PaymentSheetRedPrepaymentSet();
        PushSetEntity redPrepaymentPushSetEntity = redPrepaymentPushSet.getPushSet(corpid, PushTypeEnum.PAYMENT_SHEET_RED_PREPAYMENT_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(redPrepaymentPushSetEntity);

        List<PushSetVO> pushSetVOList = new ArrayList<>();
        BeanUtil.copyPropertiesList(basePushSetList, pushSetVOList, PushSetVO.class);
        detailPojo.setValue(pushSetVOList);
        messageSetDetailPojos.add(detailPojo);
    }

    /**
     * 付款计划 801
     *
     * @param corpid
     * @param messageSetDetailPojos
     * @param typeInt
     * @throws IllegalAccessException
     * @throws XbbException
     * @throws InstantiationException
     */
    private void getMessageSetSaasPayPlanAddDetailList(String corpid, List<MessageSetDetailPojo> messageSetDetailPojos, Integer typeInt) throws IllegalAccessException, XbbException, InstantiationException {
        MessageSetDetailPojo detailPojo = new MessageSetDetailPojo();
        detailPojo.setTitle(I18nMessageUtil.getMessage(MessageSetDetailTitleConstants.PAY_PLAN_REMIND));
        List<PushSetEntity> basePushSetList = new ArrayList<>();

        PayPlanPushSet payPlanPushSet = new PayPlanPushSet();
        PushSetEntity payPlanPushSetEntity = payPlanPushSet.getPushSet(corpid, PushTypeEnum.PAY_PLAN_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(payPlanPushSetEntity);

        PayPlanRemindPushSet payPlanRemindPushSet = new PayPlanRemindPushSet();
        PushSetEntity payPlanRemindPushSetEntity = payPlanRemindPushSet.getPushSet(corpid, PushTypeEnum.PAY_PLAN_REMIND.getSubCode(), pushSetModel);
        basePushSetList.add(payPlanRemindPushSetEntity);

        PayPlanRemindOverduePushSet payPlanRemindOverduePushSet = new PayPlanRemindOverduePushSet();
        PushSetEntity payPlanRemindOverduePushSetEntity = payPlanRemindOverduePushSet.getPushSet(corpid, PushTypeEnum.PAY_PLAN_OVERDUE.getSubCode(), pushSetModel);
        basePushSetList.add(payPlanRemindOverduePushSetEntity);

        List<PushSetVO> pushSetVOList = new ArrayList<>();
        BeanUtil.copyPropertiesList(basePushSetList, pushSetVOList, PushSetVO.class);
        detailPojo.setValue(pushSetVOList);
        messageSetDetailPojos.add(detailPojo);
    }

    /**
     * 付款单 802
     *
     * @param corpid
     * @param messageSetDetailPojos
     * @param typeInt
     * @throws IllegalAccessException
     * @throws XbbException
     * @throws InstantiationException
     */
    private void getMessageSetSaasPaySheetAddDetailList(String corpid, List<MessageSetDetailPojo> messageSetDetailPojos, Integer typeInt) throws IllegalAccessException, XbbException, InstantiationException {
        MessageSetDetailPojo detailPojo = new MessageSetDetailPojo();
        detailPojo.setTitle(I18nMessageUtil.getMessage(MessageSetDetailTitleConstants.PAY_SHEET_REMIND));
        List<PushSetEntity> basePushSetList = new ArrayList<>();

        PaySheetPushSet paySheetPushSet = new PaySheetPushSet();
        PushSetEntity paySheetPushSetEntity = paySheetPushSet.getPushSet(corpid, PushTypeEnum.PAY_SHEET_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(paySheetPushSetEntity);

        PaySheetRedPushSet paySheetRedPushSet = new PaySheetRedPushSet();
        PushSetEntity paySheetRedPushSetEntity = paySheetRedPushSet.getPushSet(corpid, PushTypeEnum.PAY_SHEET_RED_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(paySheetRedPushSetEntity);

        PaySheetBadPushSet paySheetBadPushSet = new PaySheetBadPushSet();
        PushSetEntity paySheetBadPushSetEntity = paySheetBadPushSet.getPushSet(corpid, PushTypeEnum.PAY_SHEET_BAD_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(paySheetBadPushSetEntity);

        PaySheetPrepaySet prepayPushSet = new PaySheetPrepaySet();
        PushSetEntity prepayPushSetEntity = prepayPushSet.getPushSet(corpid, PushTypeEnum.PAY_SHEET_PREPAY_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(prepayPushSetEntity);

        PaySheetRedPrepaySet redPrepayPushSet = new PaySheetRedPrepaySet();
        PushSetEntity redPrepayPushSetEntity = redPrepayPushSet.getPushSet(corpid, PushTypeEnum.PAY_SHEET_RED_PREPAY_ADD.getSubCode(), pushSetModel);
        basePushSetList.add(redPrepayPushSetEntity);

        List<PushSetVO> pushSetVOList = new ArrayList<>();
        BeanUtil.copyPropertiesList(basePushSetList, pushSetVOList, PushSetVO.class);
        detailPojo.setValue(pushSetVOList);
        messageSetDetailPojos.add(detailPojo);
    }


    /**
     * 销项发票 901
     *
     * @param corpid
     * @param messageSetDetailPojos
     * @param typeInt
     * @throws IllegalAccessException
     * @throws XbbException
     * @throws InstantiationException
     */
    private void getMessageSetSaasInvoicePushDetailList(String corpid, List<MessageSetDetailPojo> messageSetDetailPojos, Integer typeInt) throws IllegalAccessException, XbbException, InstantiationException {
        List<PushTypeEnum> pushTypeEnumList = new ArrayList<>(PushTypeEnum.getAllEnumByType(typeInt));
        List<PushTypeEnum> basePushTypeEnum = new ArrayList<>();
        for (PushTypeEnum pushTypeEnum : pushTypeEnumList) {
            if (pushTypeEnum.getSubCode() > 0) {
                basePushTypeEnum.add(pushTypeEnum);
            }
        }

        InvoicePushSet invoicePushSet = new InvoicePushSet();
        MessageSetDetailPojo detailPojo = new MessageSetDetailPojo();
        detailPojo.setTitle(I18nMessageUtil.getMessage(MessageSetDetailTitleConstants.INVOICE_BASE));
        List<PushSetEntity> basePushSetList = new ArrayList<>();
        for (PushTypeEnum pushTypeEnum : basePushTypeEnum) {
            PushSetEntity pushSetEntity = invoicePushSet.getPushSet(corpid, pushTypeEnum.getSubCode(), pushSetModel);
            basePushSetList.add(pushSetEntity);
        }

        InvoiceRedPushSet invoiceRedPushSet = new InvoiceRedPushSet();
        PushSetEntity invoiceRedPushSetEntity = invoiceRedPushSet.getPushSet(corpid, PushTypeEnum.INVOICE_RED_ADD_PUSH.getSubCode(), pushSetModel);
        basePushSetList.add(invoiceRedPushSetEntity);

        List<PushSetVO> pushSetVOList = new ArrayList<>();
        BeanUtil.copyPropertiesList(basePushSetList, pushSetVOList, PushSetVO.class);
        detailPojo.setValue(pushSetVOList);
        messageSetDetailPojos.add(detailPojo);
    }


    /**
     * 进项发票 903
     *
     * @param corpid
     * @param messageSetDetailPojos
     * @param typeInt
     * @throws IllegalAccessException
     * @throws XbbException
     * @throws InstantiationException
     */
    private void getMessageSetSaasPurchaseInvoicePushDetailList(String corpid, List<MessageSetDetailPojo> messageSetDetailPojos, Integer typeInt) throws IllegalAccessException, XbbException, InstantiationException {
        List<PushTypeEnum> pushTypeEnumList = new ArrayList<>(PushTypeEnum.getAllEnumByType(typeInt));
        List<PushTypeEnum> basePushTypeEnum = new ArrayList<>();
        for (PushTypeEnum pushTypeEnum : pushTypeEnumList) {
            if (pushTypeEnum.getSubCode() > 0) {
                basePushTypeEnum.add(pushTypeEnum);
            }
        }

        PurchaseInvoicePushSet purchaseInvoicePushSet = new PurchaseInvoicePushSet();
        MessageSetDetailPojo detailPojo = new MessageSetDetailPojo();
        detailPojo.setTitle(I18nMessageUtil.getMessage(MessageSetDetailTitleConstants.PURCHASE_INVOICE_BASE));
        List<PushSetEntity> basePushSetList = new ArrayList<>();
        for (PushTypeEnum pushTypeEnum : basePushTypeEnum) {
            PushSetEntity pushSetEntity = purchaseInvoicePushSet.getPushSet(corpid, pushTypeEnum.getSubCode(), pushSetModel);
            basePushSetList.add(pushSetEntity);
        }

        PurchaseInvoiceRedPushSet purchaseInvoiceRedPushSet = new PurchaseInvoiceRedPushSet();
        PushSetEntity invoiceRedPushSetEntity = purchaseInvoiceRedPushSet.getPushSet(corpid, PushTypeEnum.PURCHASE_INVOICE_RED_ADD_PUSH.getSubCode(), pushSetModel);
        basePushSetList.add(invoiceRedPushSetEntity);

        List<PushSetVO> pushSetVOList = new ArrayList<>();
        BeanUtil.copyPropertiesList(basePushSetList, pushSetVOList, PushSetVO.class);
        detailPojo.setValue(pushSetVOList);
        messageSetDetailPojos.add(detailPojo);
    }


    @Override
    public List<String> getUserIdListByPushSetRule(String corpid, Integer type, Integer subType, UserEntity userEntity, Entity entity) throws Exception {
        String alias = PushParentTypeEnum.getByCode(type).getAlias();
        String methodName = "get" + alias.substring(0, 1).toUpperCase() + alias.substring(1) + "UserIdList";
        Method method = this.getClass().getDeclaredMethod(methodName, String.class, Integer.class, UserEntity.class, Entity.class);
        Object userIds = method.invoke(this, corpid, subType, userEntity, entity);
        return distinct(userIds);
    }

    /**
     * 应收款 701
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getPaymentUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PaymentPlanPushSet paymentPlanPushSet = new PaymentPlanPushSet();
        PushSetEntity pushSetEntity = paymentPlanPushSet.getPushSet(corpid, subType, pushSetModel);
        Long contractId = null;
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAYMENT_PLAN_ADD.getSubCode(), subType)) {
                PaymentEntityExt paymentEntity = (PaymentEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(paymentEntity.getData());
                contractId = dataObject.getLong(PaymentEnum.CONTRACT.getAttr());
            }
            pushObjectString = pushSetEntity.getPushObject();

            if (contractId != null && contractId > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractId, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractId, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                            ContractEntityExt contractEntity = contractModel.getByKey(contractId, corpid);
                            //合同此处可能查不到
                            if (Objects.nonNull(contractEntity)) {
                                JSONObject contractDataObject = JsonHelperUtil.getJSONObject(contractEntity.getData());
                                result.add(contractDataObject.getString(ContractEnum.SIGN_PERSON.getAttr()));
                            }

                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 回款单 702
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getPaymentSheetUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PaymentSheetPushSet paymentSheetPushSet = new PaymentSheetPushSet();
        PushSetEntity pushSetEntity = paymentSheetPushSet.getPushSet(corpid, subType, pushSetModel);
        List<String> contractIds = new ArrayList<>();
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt paymentSheetEntityExt = (PaymentSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(paymentSheetEntityExt.getData());
                JSONArray contractIdArr = dataObject.getJSONArray(PaymentSheetEnum.CONTRACT.getAttr());
                if (!contractIdArr.isEmpty()) {
                    contractIds = contractIdArr.toJavaList(String.class);
                }
            }
            pushObjectString = pushSetEntity.getPushObject();

            if (contractIds != null && contractIds.size() > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            param.put("corpid", corpid);
                            param.put("idIn", contractIds);
                            List<ContractEntityExt> contracts = contractModel.findEntitys(param);
                            // todo起线程得，数据可能查不到了
                            if (Objects.nonNull(contracts) && contracts.size() > 0) {
                                contracts.stream().map(contractEntity -> JsonHelperUtil.getJSONObject(contractEntity.getData())).map(contractDataObject -> contractDataObject.getString(ContractEnum.SIGN_PERSON.getAttr())).forEach(result::add);
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 红冲回款单 703
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getRedPaymentSheetUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        Set<String> result = new HashSet<>();
        PaymentSheetRedPushSet paymentSheetRedPushSet = new PaymentSheetRedPushSet();
        PushSetEntity pushSetEntity = paymentSheetRedPushSet.getPushSet(corpid, subType, pushSetModel);
        List<String> contractIds = new ArrayList<>();
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_RED_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt entityExt = (PaymentSheetEntityExt) entity;
                JSONObject paymentDataObject = JsonHelperUtil.getJSONObject(entityExt.getData());
                JSONArray contractIdArr = paymentDataObject.getJSONArray(PaymentSheetEnum.CONTRACT.getAttr());
                if (!contractIdArr.isEmpty()) {
                    contractIds = contractIdArr.toJavaList(String.class);
                }
            }
            String pushObjectString = pushSetEntity.getPushObject();
            if (contractIds != null && contractIds.size() > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            param.put("corpid", corpid);
                            param.put("idIn", contractIds);
                            List<ContractEntityExt> contracts = contractModel.findEntitys(param);
                            // todo起线程得，数据可能查不到了
                            if (Objects.nonNull(contracts) && contracts.size() > 0) {
                                contracts.stream().map(contractEntity -> JsonHelperUtil.getJSONObject(contractEntity.getData())).map(contractDataObject -> contractDataObject.getString(ContractEnum.SIGN_PERSON.getAttr())).forEach(result::add);
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 坏账 704
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getBadDebtUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PaymentSheetBadPushSet paymentSheetBadPushSet = new PaymentSheetBadPushSet();
        PushSetEntity pushSetEntity = paymentSheetBadPushSet.getPushSet(corpid, subType, pushSetModel);
        List<String> contractIds = new ArrayList<>();
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_BAD_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt paymentSheetEntityExt = (PaymentSheetEntityExt) entity;
                JSONObject paymentDataObject = JsonHelperUtil.getJSONObject(paymentSheetEntityExt.getData());
                JSONArray contractIdArr = paymentDataObject.getJSONArray(PaymentSheetEnum.CONTRACT.getAttr());
                if (!contractIdArr.isEmpty()) {
                    contractIds = contractIdArr.toJavaList(String.class);
                }
            }
            pushObjectString = pushSetEntity.getPushObject();

            if (contractIds != null && contractIds.size() > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            param.put("corpid", corpid);
                            param.put("idIn", contractIds);
                            List<ContractEntityExt> contracts = contractModel.findEntitys(param);
                            if (Objects.nonNull(contracts) && contracts.size() > 0) {
                                contracts.stream().map(contractEntity -> JsonHelperUtil.getJSONObject(contractEntity.getData())).map(contractDataObject -> contractDataObject.getString(ContractEnum.SIGN_PERSON.getAttr())).forEach(result::add);
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 预收款 708
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getPrepaymentUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaymentSheetPrepaymentSet paymentSheetPrepaymentSet = new PaymentSheetPrepaymentSet();
        PushSetEntity pushSetEntity = paymentSheetPrepaymentSet.getPushSet(corpid, subType, pushSetModel);
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            Long customerId = null;
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_PREPAYMENT_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt paymentSheetEntityExt = (PaymentSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(paymentSheetEntityExt.getData());
                customerId = dataObject.getLong(PaymentSheetEnum.CUSTOMER_ID.getAttr());
            }
            return getCustomerMainuserCollaborator(corpid, pushSetEntity, userEntity, customerId);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 红冲预收款 711
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getRedPrepaymentUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaymentSheetRedPrepaymentSet paymentSheetRedPrepaymentSet = new PaymentSheetRedPrepaymentSet();
        PushSetEntity pushSetEntity = paymentSheetRedPrepaymentSet.getPushSet(corpid, subType, pushSetModel);
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            Long customerId = null;
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_RED_PREPAYMENT_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt paymentSheetEntityExt = (PaymentSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(paymentSheetEntityExt.getData());
                customerId = dataObject.getLong(PaymentSheetEnum.CUSTOMER_ID.getAttr());
            }
            return getCustomerMainuserCollaborator(corpid, pushSetEntity, userEntity, customerId);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 付款计划 801
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getPayPlanUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PayPlanPushSet payPlanPushSet = new PayPlanPushSet();
        PushSetEntity pushSetEntity = payPlanPushSet.getPushSet(corpid, subType, pushSetModel);
        Long purchaseId = null;
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAY_PLAN_ADD.getSubCode(), subType)) {
                PayPlanEntityExt payPlanEntity = (PayPlanEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(payPlanEntity.getData());
                purchaseId = dataObject.getLong(PayPlanEnum.LINK_PURCHASE.getAttr());
            }
            pushObjectString = pushSetEntity.getPushObject();

            if (purchaseId != null && purchaseId > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseId, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseId, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.PURCHASE_SIGNING.getAlias())) {
                            PurchaseEntityExt purchaseEntity = purchaseModel.getByKey(purchaseId, corpid);
                            if (Objects.nonNull(purchaseEntity)) {
                                JSONObject purchaseDataObject = JsonHelperUtil.getJSONObject(purchaseEntity.getData());
                                result.add(purchaseDataObject.getString(PurchaseEnum.SIGNER_ID.getAttr()));
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 付款单 802
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getPaySheetUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetPushSet paySheetPushSet = new PaySheetPushSet();
        PushSetEntity pushSetEntity = paySheetPushSet.getPushSet(corpid, subType, pushSetModel);
        return getPaySheetUserIdList(corpid, subType, userEntity, entity, pushSetEntity, PushTypeEnum.PAY_SHEET_ADD);
    }

    private List<String> getPaySheetUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity, PushSetEntity pushSetEntity, PushTypeEnum paySheetPushTypeEnum) {
        if (Objects.equals(pushSetEntity.getEnable(), BasicConstant.ONE)) {
            List<String> contractIds = new ArrayList<>();
            if (Objects.equals(paySheetPushTypeEnum.getSubCode(), subType)) {
                PayPlanSheetEntityExt entityExt = (PayPlanSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(entityExt.getData());
                JSONArray contractIdArr = dataObject.getJSONArray(PaySheetEnum.LINK_PURCHASE.getAttr());
                if (!contractIdArr.isEmpty()) {
                    contractIds = contractIdArr.toJavaList(String.class);
                }
            }
            String pushObjectString = pushSetEntity.getPushObject();
            Set<String> result = new HashSet<>();
            if (contractIds != null && contractIds.size() > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, contractIds, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, contractIds, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.PURCHASE_SIGNING.getAlias())) {
                            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            param.put("corpid", corpid);
                            param.put("idIn", contractIds);
                            List<PurchaseEntityExt> contracts = purchaseModel.findEntitys(param);
                            // todo起线程得，数据可能查不到了
                            if (Objects.nonNull(contracts) && contracts.size() > 0) {
                                contracts.stream().map(purchaseEntity -> JsonHelperUtil.getJSONObject(purchaseEntity.getData())).map(contractDataObject -> contractDataObject.getString(PurchaseEnum.SIGNER_ID.getAttr())).forEach(result::add);
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 预付款 805
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getPrepayUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetPrepaySet paySheetPrepaySet = new PaySheetPrepaySet();
        PushSetEntity pushSetEntity = paySheetPrepaySet.getPushSet(corpid, subType, pushSetModel);
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            Long supplierId = null;
            if (Objects.equals(PushTypeEnum.PAY_SHEET_PREPAY_ADD.getSubCode(), subType)) {
                PayPlanSheetEntityExt payPlanSheetEntityExt = (PayPlanSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(payPlanSheetEntityExt.getData());
                supplierId = dataObject.getLong(PaySheetEnum.LINK_SUPPLIER.getAttr());
            }
            return getSupplierMainuserCollaborator(corpid, pushSetEntity, userEntity, supplierId);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 红冲已核销 806
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getRedWriteOffPayUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetRedPushSet paySheetRedPushSet = new PaySheetRedPushSet();
        PushSetEntity pushSetEntity = paySheetRedPushSet.getPushSet(corpid, subType, pushSetModel);
        return getPaySheetUserIdList(corpid, subType, userEntity, entity, pushSetEntity, PushTypeEnum.PAY_SHEET_RED_ADD);
    }

    /**
     * 红冲预付款 808
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getRedPrepayUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetRedPrepaySet paySheetRedPrepaySet = new PaySheetRedPrepaySet();
        PushSetEntity pushSetEntity = paySheetRedPrepaySet.getPushSet(corpid, subType, pushSetModel);
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            Long supplierId = null;
            if (Objects.equals(PushTypeEnum.PAY_SHEET_RED_PREPAY_ADD.getSubCode(), subType)) {
                PayPlanSheetEntityExt payPlanSheetEntityExt = (PayPlanSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(payPlanSheetEntityExt.getData());
                supplierId = dataObject.getLong(PaySheetEnum.LINK_SUPPLIER.getAttr());
            }
            return getSupplierMainuserCollaborator(corpid, pushSetEntity, userEntity, supplierId);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 坏账付款单 809
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getBadPayUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetRedPushSet paySheetRedPushSet = new PaySheetRedPushSet();
        PushSetEntity pushSetEntity = paySheetRedPushSet.getPushSet(corpid, subType, pushSetModel);
        return getPaySheetUserIdList(corpid, subType, userEntity, entity, pushSetEntity, PushTypeEnum.PAY_SHEET_BAD_ADD);
    }

    /**
     * 发票 901
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getInvoiceUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        InvoiceEntityExt invoiceEntity = (InvoiceEntityExt) entity;
        InvoicePushSet invoicePushSet = new InvoicePushSet();
        PushSetEntity pushSetEntity = invoicePushSet.getPushSet(corpid, subType, pushSetModel);

        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", invoiceEntity.getCorpid());
        params.put("invoiceId", invoiceEntity.getId());
        params.put("del", 0);

        List<InvoiceRelationshipEntity> invoiceRelationshipEntityList = invoiceRelationshipModel.findEntitys(params);
        Set<Long> customerIds = new HashSet<>();
        Set<String> contractIds = new HashSet<>();
        for (InvoiceRelationshipEntity invoiceRelationshipEntity : invoiceRelationshipEntityList) {
            customerIds.add(invoiceRelationshipEntity.getCustomerId());
            if (!Objects.equals(invoiceRelationshipEntity.getContractId(), 0L)) {
                contractIds.add(invoiceRelationshipEntity.getContractId().toString());
            }
        }

        List<Long> customerIdList = new ArrayList<>(customerIds);
        List<String> contractIdList = new ArrayList<>(contractIds);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushOject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushOject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        if (customerIdList.size() > 0) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, customerIdList, 1));
                        }
                        if (contractIdList.size() > 0) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIdList, 1));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        if (customerIdList.size() > 0) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, customerIdList, 0));
                        }
                        if (contractIdList.size() > 0) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIdList, 0));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                        if (contractIdList.size() > 0) {
                            result.addAll(contractModel.getContractJsonExtractListByIn(corpid, contractIdList, ContractEnum.SIGN_PERSON.getAttr()));
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 红冲发票 902
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getRedInvoiceUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        InvoiceEntityExt invoiceEntity = (InvoiceEntityExt) entity;
        InvoiceRedPushSet invoiceRedPushSet = new InvoiceRedPushSet();
        PushSetEntity pushSetEntity = invoiceRedPushSet.getPushSet(corpid, subType, pushSetModel);

        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", invoiceEntity.getCorpid());
        params.put("invoiceId", invoiceEntity.getId());
        params.put("del", 0);

        List<InvoiceRelationshipEntity> invoiceRelationshipEntityList = invoiceRelationshipModel.findEntitys(params);
        Set<Long> customerIds = new HashSet<>();
        Set<String> contractIds = new HashSet<>();
        for (InvoiceRelationshipEntity invoiceRelationshipEntity : invoiceRelationshipEntityList) {
            customerIds.add(invoiceRelationshipEntity.getCustomerId());
            if (!Objects.equals(invoiceRelationshipEntity.getCustomerId(), 0L)) {
                contractIds.add(invoiceRelationshipEntity.getContractId().toString());
            }
        }
        List<Long> customerIdList = new ArrayList<>(customerIds);
        List<String> contractIdList = new ArrayList<>(contractIds);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushOject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushOject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        if (customerIdList.size() > 0) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, customerIdList, 1));
                        }
                        if (contractIdList.size() > 0) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIdList, 1));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        if (customerIdList.size() > 0) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, customerIdList, 0));
                        }
                        if (contractIdList.size() > 0) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIdList, 0));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                        if (contractIdList.size() > 0) {
                            result.addAll(contractModel.getContractJsonExtractListByIn(corpid, contractIdList, ContractEnum.SIGN_PERSON.getAttr()));
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 进项发票 903
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getPurchaseInvoiceUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PurchaseInvoiceEntityExt purchaseInvoiceEntity = (PurchaseInvoiceEntityExt) entity;
        PurchaseInvoicePushSet purchaseInvoicePushSet = new PurchaseInvoicePushSet();
        PushSetEntity pushSetEntity = purchaseInvoicePushSet.getPushSet(corpid, subType, pushSetModel);

        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", purchaseInvoiceEntity.getCorpid());
        params.put("purchaseInvoiceId", purchaseInvoiceEntity.getId());
        params.put("del", 0);

        List<PurchaseInvoiceRelationshipEntity> purchaseInvoiceRelationshipEntityList = purchaseInvoiceRelationshipModel.findEntitys(params);
        Set<Long> supplierIds = new HashSet<>();
        Set<String> purchaseIds = new HashSet<>();
        for (PurchaseInvoiceRelationshipEntity purchaseInvoiceRelationshipEntity : purchaseInvoiceRelationshipEntityList) {
            supplierIds.add(purchaseInvoiceRelationshipEntity.getSupplierId());
            if (!Objects.equals(purchaseInvoiceRelationshipEntity.getPurchaseId(), 0L)) {
                purchaseIds.add(purchaseInvoiceRelationshipEntity.getPurchaseId().toString());
            }
        }

        List<Long> supplierIdList = new ArrayList<>(supplierIds);
        List<String> purchaseIdList = new ArrayList<>(purchaseIds);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushOject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushOject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        if (supplierIdList.size() > 0) {
                            result.addAll(supplierUserModel.getSupplierUserIdList(corpid, supplierIdList, 1));
                        }
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseIdList, 1));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        if (supplierIdList.size() > 0) {
                            result.addAll(supplierUserModel.getSupplierUserIdList(corpid, supplierIdList, 0));
                        }
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseIdList, 0));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.PURCHASE_SIGNING.getAlias())) {
                        if (purchaseIdList.size() > 0) {
                            List<String> siginList = purchaseModel.getPurchaseJsonExtractListByIn(corpid, purchaseIdList, PurchaseEnum.SIGNER_ID.getAttr());
                            result.addAll(siginList);
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 红冲进项发票 904
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getRedPurchaseInvoiceUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PurchaseInvoiceEntityExt purchaseInvoiceEntity = (PurchaseInvoiceEntityExt) entity;
        PurchaseInvoiceRedPushSet purchaseInvoiceRedPushSet = new PurchaseInvoiceRedPushSet();
        PushSetEntity pushSetEntity = purchaseInvoiceRedPushSet.getPushSet(corpid, subType, pushSetModel);

        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", purchaseInvoiceEntity.getCorpid());
        params.put("purchaseInvoiceId", purchaseInvoiceEntity.getId());
        params.put("del", 0);

        List<PurchaseInvoiceRelationshipEntity> purchaseInvoiceRelationshipEntityList = purchaseInvoiceRelationshipModel.findEntitys(params);
        Set<Long> supplierIds = new HashSet<>();
        Set<String> purchaseIds = new HashSet<>();
        for (PurchaseInvoiceRelationshipEntity purchaseInvoiceRelationshipEntity : purchaseInvoiceRelationshipEntityList) {
            supplierIds.add(purchaseInvoiceRelationshipEntity.getSupplierId());
            if (!Objects.equals(purchaseInvoiceRelationshipEntity.getPurchaseId(), 0L)) {
                purchaseIds.add(purchaseInvoiceRelationshipEntity.getPurchaseId().toString());
            }
        }

        List<Long> supplierIdList = new ArrayList<>(supplierIds);
        List<String> purchaseIdList = new ArrayList<>(purchaseIds);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushOject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushOject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        if (supplierIdList.size() > 0) {
                            result.addAll(supplierUserModel.getSupplierUserIdList(corpid, supplierIdList, 1));
                        }
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseIdList, 1));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        if (supplierIdList.size() > 0) {
                            result.addAll(supplierUserModel.getSupplierUserIdList(corpid, supplierIdList, 0));
                        }
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseIdList, 0));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseModel.getPurchaseJsonExtractListByIn(corpid, purchaseIdList, PurchaseEnum.SIGNER_ID.getAttr()));
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 对账单 5403
     *
     * @param corpid
     * @param subType
     * @param userEntity
     * @param entity
     * @return
     */
    private List<String> getCustomerStatementStatisticUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        List<String> userIdList = new ArrayList<>();
        // 经销商负责人、协同人
        CustomerStatementStatisticsEntity statementEntity = (CustomerStatementStatisticsEntity) entity;
        long customerId = statementEntity.getData().getLongValue(CustomerStatementStatisticDistributorEnum.CUSTOMER_ID.getAttr());
        userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
        userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
        userIdList.removeIf(item -> Objects.equals(item, userEntity.getUserId()));
        return userIdList;
    }

    /**
     * 获取客户负责人和协同人
     *
     * @param corpid
     * @param pushSetEntity
     * @param userEntity
     * @param customerId
     * @return
     */
    private List<String> getCustomerMainuserCollaborator(String corpid, PushSetEntity pushSetEntity, UserEntity userEntity, Long customerId) {
        Set<String> userIdList = new HashSet<>();
        String pushObjectString = pushSetEntity.getPushObject();
        if (customerId != null && customerId > 0) {
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
        } else {
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
        }
    }

    /**
     * 获取供应商的负责人和协同人
     *
     * @param corpid
     * @param pushSetEntity
     * @param userEntity
     * @param supplierId
     * @return
     */
    private List<String> getSupplierMainuserCollaborator(String corpid, PushSetEntity pushSetEntity, UserEntity userEntity, Long supplierId) {
        Set<String> userIdList = new HashSet<>();
        String pushObjectString = pushSetEntity.getPushObject();
        if (supplierId != null && supplierId > 0) {
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        userIdList.addAll(supplierUserModel.getSupplierUserIdList(corpid, Collections.singletonList(supplierId), 1));
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        userIdList.addAll(supplierUserModel.getSupplierUserIdList(corpid, Collections.singletonList(supplierId), 0));
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
        } else {
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
        }
    }

/*    if (Objects.equals(PushTypeEnum.PAYMENT_PLAN_ADD.getCode(), type)) {
        userIds = getPaymentUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_ADD.getCode(), type)) {
        userIds = getPaymentSheetUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_RED_ADD.getCode(), type)) {
        userIds = getRedPaymentSheetUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_BAD_ADD.getCode(), type)) {
        userIds = getBadDebtUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_PREPAYMENT_ADD.getCode(), type)) {
        userIds = getPrepaymentUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_RED_PREPAYMENT_ADD.getCode(), type)) {
        userIds = getRedPrepaymentUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAY_PLAN_ADD.getCode(), type)) {
        userIds = getPayPlanUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAY_SHEET_ADD.getCode(), type)) {
        userIds = getPaySheetUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAY_SHEET_PREPAY_ADD.getCode(), type)) {
        userIds = getPrepayUserIdList(corpid, subType, userEntity, entity);
    }  else if (Objects.equals(PushTypeEnum.PAY_SHEET_RED_ADD.getCode(), type)) {
        userIds = getRedWriteOffPayUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAY_SHEET_RED_PREPAY_ADD.getCode(), type)) {
        userIds = getRedPrepayUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PAY_SHEET_BAD_ADD.getCode(), type)) {
        userIds = getBadPayUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.INVOICE_PUSH.getCode(), type)) {
        userIds = getInvoiceUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.INVOICE_RED_ADD_PUSH.getCode(), type)) {
        userIds = getRedInvoiceUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PURCHASE_INVOICE_PUSH.getCode(), type)) {
        userIds = getPurchaseInvoiceUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.PURCHASE_INVOICE_RED_ADD_PUSH.getCode(), type)) {
        userIds = getRedPurchaseInvoiceUserIdList(corpid, subType, userEntity, entity);
    } else if (Objects.equals(PushTypeEnum.STATEMENT_GENERATE_PUSH.getCode(), type)){
        userIds = getCustomerStatementStatisticUserIdList(corpid, subType, userEntity, entity);
    }*/
}
